/*
 * @Descripttion:
 * @Author: sueRimn
 * @Date: 2019-08-09 15:23:53
 * @LastEditors: Lucia
 * @LastEditTime: 2019-08-09 17:28:13
 * @msg:
 */
import { types } from "mobx-state-tree"
import { storage } from "../utils"
import { Alert } from "../components"

import http from "../services/api/apiLibrary"

export const LoginStoreModel = types
  .model("LoginStoreModel", {
    // 账号密码登录
    username: types.optional(types.string, ""),
    password: types.optional(types.string, ""),
    usernameIsOpenClear: types.optional(types.boolean, false),
    passwordIsOpenClear: types.optional(types.boolean, false),
    isPasswordOpen: types.optional(types.boolean, false),
    rememberPswCheck: types.optional(types.boolean, false),
    // 手机验证码登录
    mobile: types.optional(types.string, ""),
    vertify: types.optional(types.string, ""),
    mobileIsOpenClear: types.optional(types.boolean, false),
    vertifyIsOpenClear: types.optional(types.boolean, false),
    xAuth: ""
  })
  .views(self => ({}))
  .actions((self: any) => {
    return {
      usernameOnChange(text: string) {
        self.username = text
        self.username.length > 0
          ? (self.usernameIsOpenClear = true)
          : (self.usernameIsOpenClear = false)
      },

      passwordOnChange(text: string) {
        self.password = text
        self.password.length > 0
          ? (self.passwordIsOpenClear = true)
          : (self.passwordIsOpenClear = false)
      },

      usernameClear() {
        self.username = ""
        self.usernameIsOpenClear = false
      },

      passwordClear() {
        self.password = ""
        self.passwordIsOpenClear = false
      },

      changePasswordStyle() {
        self.isPasswordOpen = !self.isPasswordOpen
      },

      rememberPswChange() {
        self.rememberPswCheck = !self.rememberPswCheck
      },

      mobileOnChange(text) {
        self.mobile = text
        self.mobile.length > 0 ? (self.mobileIsOpenClear = true) : (self.mobileIsOpenClear = false)
      },

      mobileClear() {
        self.mobile = ""
        self.mobileIsOpenClear = false
      },

      login(history, rootStore) {
        const param = {
          username: self.username,
          password: self.password,
          productType: 1,
          code: "",
          validate: "",
          uuid: 1808769079
        }
        http.post("/api/gl/login/username", param).then((data: any) => {
          const res = data.data
          if (res.code === 1) {
            http.setAuth(res.data.token, res.data.id)
            this.loginJump()
          } else {
            Alert({
              title: "提示",
              message: res.message,
              text: "OK",
              onPress: () => console.log("OK Pressed")
            })
          }
        })
      },

      async setStorageValue(name: string, value: string) {
        await storage.saveString(name, value)
      },

      loginJump() {
        const param = {
          gameId: 3,
          type: 0
        }
        http.post("/api/game/jump", param).then((data: any) => {
          http.setXJProxy()
          http.getXJ(data.data.data).then((data: any) => {
            this.getXJParam()
          })
        })
      },

      getXJParam() {
        const param = {
          pid: 0,
          v: 0
        }

        http.postXJParam("/zh-cn/serv/getmenu", param).then(data => {
          console.log(data)
        })
      },

      /**
       * 快捷登陆
       * @param userName
       * @param passWord
       */
      loginAndXJ(userName, passWord) {
        const param = {
          username: userName,
          password: passWord
        }
        http
          .post("/api/gl/login/username", param)
          .then((data: any) => {
            const res = data.data
            http.setAuth(res.data.token, res.data.id)
            const param = {
              gameId: 3,
              type: 0
            }
            return http.post("/api/game/jump", param)
          })
          .then((data: any) => {
            http.setXJProxy()
            return http.getXJ(data.data.data)
          })
          .then((data: any) => {
            const param = {
              pid: 0,
              v: 0
            }
            return http.postXJParam("/zh-cn/serv/getmenu", param)
          })
          .then(data => {
            console.log(data)
          })
          .catch(error => {
            Alert({
              title: "提示",
              message: error,
              text: "OK",
              onPress: () => console.log("OK Pressed")
            })
          })
      }
    }
  })
